const mongoUtil = require("../utils/mongo_util")
const collectionName = "user"
// 获取用户
function getuser(account, callback) {
    const mmp = mongoUtil.getCollection(collectionName)
    mmp.then(collection => {
        if (account) {
            collection.find({
                    'account': account
                })
                // .limit(Number(user.pagesize)).skip((Number(user.page) - 1) * Number(user.pagesize))
                .toArray(function (err, result) { // 返回集合中所有数据
                    if (result.length) {
                        // console.log("gggggg");
                        callback(null, result)
                        new Error()
                    } else {
                        // console.log("hhhhhhhh");
                        callback(err ? err : {
                            reason: "没有数据"
                        }, result)
                    }
                });
        } else {
            collection.find()
                // .limit(Number(user.pagesize)).skip((Number(user.page) - 1) * Number(user.pagesize))
                .toArray(function (err, result) { // 返回集合中所有数据
                    if (result.length) {
                        // console.log("gggggg");
                        callback(null, result)
                        new Error()
                    } else {
                        // console.log("hhhhhhhh");
                        callback(err ? err : {
                            reason: "没有数据"
                        }, result)
                    }
                });
        }

    })
}

function adduser(userObj, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.find({
            'account': userObj.account
        }).toArray(function (err, result) {
            if (result.length > 0) {
                callback({
                    msg: "已存在"
                }, result)
                return
            }
            mongoUtil.getCollection(collectionName).then(collection => {
                collection.insertOne(userObj, function (err, res) {
                    callback(err, res)
                });
            })
        })
    })
}
function patchuser(userObj, callback) {

        const whereStr = {
            'account': userObj.account,
        }
        const updateStr = {
            $set: {
                username: userObj.username,
                account: userObj.account,
                password: userObj.password,
                userTypes: userObj.userTypes,
                level: userObj.level
            }
        };
        mongoUtil.getCollection(collectionName).then(collection => {
            collection.updateOne(whereStr, updateStr, function (err, res) {
                callback(err, res)
            });
        })
  
}

function deleteuser(useraccount, callback) {
    mongoUtil.getCollection(collectionName).then(collection => {
        collection.deleteMany({
            account: {
                $in: useraccount
            }
        }, function (err, res) {
            callback(err, res)
        });
    })
}
module.exports = {
    getuser,
    adduser,
    patchuser,
    deleteuser
}